// 验证工具函数

// 验证手机号
export const validatePhone = (phone) => {
  if (!phone) return false
  const phoneRegex = /^1[3-9]\d{9}$/
  return phoneRegex.test(phone)
}

// 验证邮箱
export const validateEmail = (email) => {
  if (!email) return false
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  return emailRegex.test(email)
}

// 验证身份证号
export const validateIdCard = (idCard) => {
  if (!idCard) return false
  // 简化版身份证验证，实际应用中可能需要更复杂的验证逻辑
  const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return idCardRegex.test(idCard)
}

// 验证URL
export const validateUrl = (url) => {
  if (!url) return false
  try {
    new URL(url)
    return true
  } catch (error) {
    return false
  }
}

// 验证IP地址
export const validateIp = (ip) => {
  if (!ip) return false
  const ipRegex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
  return ipRegex.test(ip)
}

// 验证端口号
export const validatePort = (port) => {
  if (port === null || port === undefined) return false
  const portNum = parseInt(port)
  return !isNaN(portNum) && portNum >= 0 && portNum <= 65535
}

// 验证数字（整数）
export const validateInteger = (num) => {
  if (num === null || num === undefined) return false
  const numRegex = /^-?\d+$/ 
  return numRegex.test(num)
}

// 验证数字（小数）
export const validateDecimal = (num) => {
  if (num === null || num === undefined) return false
  const numRegex = /^-?\d+(\.\d+)?$/ 
  return numRegex.test(num)
}

// 验证正整数
export const validatePositiveInteger = (num) => {
  if (num === null || num === undefined) return false
  const numRegex = /^\d+$/ 
  return numRegex.test(num) && parseInt(num) > 0
}

// 验证正小数
export const validatePositiveDecimal = (num) => {
  if (num === null || num === undefined) return false
  const numRegex = /^\d+(\.\d+)?$/ 
  return numRegex.test(num) && parseFloat(num) > 0
}

// 验证中文
export const validateChinese = (str) => {
  if (!str) return false
  const chineseRegex = /^[\u4e00-\u9fa5]+$/
  return chineseRegex.test(str)
}

// 验证英文
export const validateEnglish = (str) => {
  if (!str) return false
  const englishRegex = /^[a-zA-Z]+$/
  return englishRegex.test(str)
}

// 验证英文和数字
export const validateEnglishNumber = (str) => {
  if (!str) return false
  const regex = /^[a-zA-Z0-9]+$/
  return regex.test(str)
}

// 验证密码（至少包含字母和数字，长度8-20位）
export const validatePassword = (password) => {
  if (!password) return false
  const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/
  return passwordRegex.test(password)
}

// 验证用户名（字母开头，包含字母、数字、下划线，长度3-20位）
export const validateUsername = (username) => {
  if (!username) return false
  const usernameRegex = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/
  return usernameRegex.test(username)
}

// 验证日期格式（YYYY-MM-DD）
export const validateDate = (date) => {
  if (!date) return false
  const dateRegex = /^\d{4}-\d{2}-\d{2}$/
  if (!dateRegex.test(date)) return false
  
  // 验证日期的有效性
  const [year, month, day] = date.split('-').map(Number)
  const d = new Date(year, month - 1, day)
  return d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day
}

// 验证时间格式（HH:mm:ss）
export const validateTime = (time) => {
  if (!time) return false
  const timeRegex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
  return timeRegex.test(time)
}

// 验证日期时间格式（YYYY-MM-DD HH:mm:ss）
export const validateDateTime = (dateTime) => {
  if (!dateTime) return false
  const dateTimeRegex = /^\d{4}-\d{2}-\d{2} ([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
  if (!dateTimeRegex.test(dateTime)) return false
  
  // 验证日期时间的有效性
  const [datePart, timePart] = dateTime.split(' ')
  return validateDate(datePart) && validateTime(timePart)
}

// 验证是否为空
export const isEmpty = (value) => {
  if (value === null || value === undefined) return true
  if (typeof value === 'string') return value.trim() === ''
  if (Array.isArray(value)) return value.length === 0
  if (typeof value === 'object') return Object.keys(value).length === 0
  return false
}

// 验证是否不为空
export const isNotEmpty = (value) => {
  return !isEmpty(value)
}

// 验证长度范围
export const validateLength = (str, minLength, maxLength) => {
  if (str === null || str === undefined) return false
  const length = String(str).length
  return length >= minLength && length <= maxLength
}

// 验证最大值
export const validateMax = (num, max) => {
  if (num === null || num === undefined) return false
  const numValue = Number(num)
  return !isNaN(numValue) && numValue <= max
}

// 验证最小值
export const validateMin = (num, min) => {
  if (num === null || num === undefined) return false
  const numValue = Number(num)
  return !isNaN(numValue) && numValue >= min
}

// 验证范围
export const validateRange = (num, min, max) => {
  return validateMin(num, min) && validateMax(num, max)
}